package com.whales.shared.util;
import java.util.*;
import java.io.*;

/**
 * @author Andreas Norman
 * @version 1.0
 *
 */
@SuppressWarnings("serial")
public class JIniFile extends ArrayList<String> {
	private File userFileName;


	public JIniFile(String FileName) {
		clear();
		File userFileName = new File(FileName);
		this.userFileName = userFileName;
		if (userFileName.exists()) {
			try {
				BufferedReader inbuf = new BufferedReader(new FileReader(this.userFileName));
				while (true) {
					String s = inbuf.readLine();
					if (s == null) {
						break;
					}
					add(s);
				}
				inbuf.close();
			} catch ( IOException ioe ) {}
		} else {
			try {
				BufferedReader inbuf = new BufferedReader(new FileReader(this.userFileName));
				inbuf.close();
			} catch ( IOException ioe ) {}
		}
	}


	public ArrayList<String> ReadSections() {
		ArrayList<String> list = new ArrayList<String>();
		String s;
		for (int i=0; i < size(); i++) {
			s = get(i).toString();
			if (s.startsWith("[") && s.endsWith("]")) {
				list.add(s.substring(1,(s.length()-1)));
			}
		}
		return list;
	}


	public boolean UpdateFile() {

		BufferedWriter outbuf = null;
		boolean bReturn = true;
		try {
			outbuf = new BufferedWriter(new FileWriter(this.userFileName, false));
			int i=0;
			while (i<size()) {
				String s = get(i).toString();
				if (s == null) {
					break;
				}
				outbuf.write(s);
				outbuf.newLine();
				i++;
			}

		} catch ( IOException ioe ) {
			bReturn = false;
		}
		finally{
			if(outbuf!=null)
				try {
					outbuf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

		return bReturn;
	}


	public String ReadString(String Section, String key, String defaultValue) {
		String value=defaultValue;
		if (ValuePosition(Section, key) > 0) {
			int strLen = key.length()+1;
			value = get(ValuePosition(Section, key)).toString().substring(strLen, get(ValuePosition(Section, key)).toString().length());
		}
		return value;
	}


	public ArrayList<String> ReadSectionValues(String Section) {
		ArrayList<String> myList = new ArrayList<String>();
		int start = this.SectionPosition(Section)+1;
		String s;
		if (this.SectionPosition(Section) > -1) {
			for (int i=start; i < size(); i++) {
				s = get(i).toString().substring(get(i).toString().indexOf("=")+1, get(i).toString().length());
				if (s.startsWith("[") && s.endsWith("]")) {
					break;
				} else {
					myList.add(s);
				}
			}
		}
		return myList;
	}


	public int ReadInteger(String Section, String key, int defaultValue) {
		int value=defaultValue;
		if (ValuePosition(Section, key) > 0) {
			int strLen = key.length()+1;
			value = Integer.parseInt(get(ValuePosition(Section, key)).toString().substring(strLen, get(ValuePosition(Section, key)).toString().length()));
		}
		return value;
	}


	public Float ReadFloat(String Section, String key, Float defaultValue) {
		Float value = new Float(0f);
		value = defaultValue;
		if (ValuePosition(Section, key) > 0) {
			int strLen = key.length()+1;
			value = Float.valueOf(get(ValuePosition(Section, key)).toString().substring(strLen, get(ValuePosition(Section, key)).toString().length()));
		}
		return value;
	}


	public boolean ReadBool(String Section, String key, boolean defaultValue) {
		boolean value=defaultValue;
		if (ValuePosition(Section, key) > 0) {
			int strLen = key.length()+1;
			value = Boolean.getBoolean(get(ValuePosition(Section, key)).toString().substring(strLen, get(ValuePosition(Section, key)).toString().length()));
		}
		return value;
	}


	private int SectionPosition(String Section) {
		String s;
		int pos = -1;
		for (int i=0; i < size(); i++) {
			s = get(i).toString();
			if (s.equals("["+Section+"]")) {
				pos = i;
				break;
			}
		}
		return pos;
	}


	public boolean SectionExist(String Section) {
		String s;
		boolean val = false;
		for (int i=0; i < size(); i++) {
			s = get(i).toString();
			if (s.equals("["+Section+"]")) {
				val = true;
				break;
			}
		}
		return val;
	}


	public void EraseSection(String Section) {
		String s;
		int start = this.SectionPosition(Section)+1;
		if (this.SectionPosition(Section) > -1) {
			for (int i=start; i < size(); i++) {
				s = get(i).toString();
				if (s.startsWith("[") && s.endsWith("]")) {
					break;
				} else {
					remove(i);
					i--;
				}
			}
			remove(this.SectionPosition(Section));
		}
	}


	public ArrayList<String> ReadSection(String Section) {
		String s;
		ArrayList<String> myList = new ArrayList<String>();
		int start = this.SectionPosition(Section)+1;
		if (this.SectionPosition(Section) > -1) {
			for (int i=start; i < size(); i++) {
				s = get(i).toString();
				if (s.startsWith("[") && s.endsWith("]")) {
					break;
				} else {
					myList.add(s.substring(0, s.indexOf("=")));
				}
			}
		}
		return myList;
	}


	public boolean ValueExist(String Section, String key) {
		int start = SectionPosition(Section);
		String s;
		boolean val = false;

		for (int i=start+1; i < size(); i++) {
			s = get(i).toString();
			if (s.startsWith(key+"=")) {
				val = true;
				break;
			} else if (s.startsWith("[") && s.endsWith("]")) {
				break;
			}
		}
		return val;
	}


	private int ValuePosition(String Section, String key) {
		int start = SectionPosition(Section);
		String s;
		int pos =-1;

		for (int i=start+1; i < size(); i++) {
			s = get(i).toString();
			if (s.startsWith(key+"=")) {
				pos = i;
				break;
			} else if (s.startsWith("[") && s.endsWith("]")) {
				break;
			}
		}
		return pos;
	}


	public void DeleteKey(String Section, String key) {
		if (this.ValuePosition(Section, key) > 0) {
			remove(this.ValuePosition(Section, key));
		}
	}


	public void WriteString(String Section, String key, String value) {
		String s = key+"="+value;
		this.addToList(Section, key, s);
	}



	public void WriteBool(String Section, String key, boolean value) {
		String s = key+"="+Boolean.toString(value);
		this.addToList(Section, key, s);
	}


	public void WriteFloat(String Section, String key, float value) {
		String s = key+"="+Float.toString(value);
		this.addToList(Section, key, s);
	}


	public void WriteInteger(String Section, String key, int value) {
		String s = key+"="+Integer.toString(value);
		this.addToList(Section, key, s);
	}


	private void addToList(String Section, String key, String value) {
		if (this.SectionExist((Section))) {
			if (this.ValueExist(Section, key)) {
				int pos = this.ValuePosition(Section, key);
				remove(pos);
				add(pos, value);
			} else {
				add((this.SectionPosition(Section)+1),value);
			}
		} else {
			add("["+Section+"]");
			add(value);
			;
		}
	}
}

